package edu.cmu.cs.stage3.alice.authoringtool.util;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;

/**
 * @author Jason Pratt
 */
public class WatcherPanel extends javax.swing.JPanel /*implements edu.cmu.cs.stage3.alice.core.event.PropertyListener, edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyListener, edu.cmu.cs.stage3.alice.core.event.ChildrenListener*/ {

 protected java.util.List variablesToWatch = new java.util.ArrayList();
 protected java.util.List propertiesToWatch = new java.util.ArrayList();
 protected edu.cmu.cs.stage3.alice.core.Element root;

 public WatcherPanel () {
  setBackground( java.awt.Color.white );
  setLayout( new java.awt.GridBagLayout() );
  addContainerListener( GUIElementContainerListener.getStaticListener() );
  }

 // Is there some variable with "watching" activated?
 public boolean isThereSomethingToWatch () {
  return (variablesToWatch.size() + propertiesToWatch.size()) > 0;
  }

 public void clear () {
  removeAllVariablesBeingWatched();
  removeAllPropertiesBeingWatched();
  }

 public void addVariableToWatch (edu.cmu.cs.stage3.alice.core.Variable variable) {
  variablesToWatch.add( variable );
  refreshGUI();
  }

 public void removeVariableBeingWatched (edu.cmu.cs.stage3.alice.core.Variable variable) {
  variablesToWatch.remove( variable );
  refreshGUI();
  }

 public void removeAllVariablesBeingWatched () {
  variablesToWatch.clear();
  refreshGUI();
  }

 public boolean isVariableBeingWatched (edu.cmu.cs.stage3.alice.core.Variable variable) {
  return variablesToWatch.contains( variable );
  }

 public void addPropertyToWatch (edu.cmu.cs.stage3.alice.core.Property property) {
  propertiesToWatch.add( property );
  refreshGUI();
  }

 public void removePropertyBeingWatched (edu.cmu.cs.stage3.alice.core.Property property) {
  propertiesToWatch.remove( property );
  refreshGUI();
  }

 public void removeAllPropertiesBeingWatched () {
  propertiesToWatch.clear();
  refreshGUI();
  }

 public boolean isPropertyBeingWatched (edu.cmu.cs.stage3.alice.core.Property property) {
  return propertiesToWatch.contains( property );
  }

 protected void refreshGUI () {
  this.removeAll();
  int count = 0;
  for (java.util.Iterator iter = variablesToWatch.iterator(); iter.hasNext(); ) {
   edu.cmu.cs.stage3.alice.core.Variable variable = (edu.cmu.cs.stage3.alice.core.Variable)iter.next();
   edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory factory = new edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory( variable.value );
   javax.swing.JComponent gui = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getVariableGUI( variable, true, factory );
   if (gui != null) {
    this.add( gui, new java.awt.GridBagConstraints( 0, count++, 1, 1, 1.0, 0.0, java.awt.GridBagConstraints.WEST, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 2, 2, 2, 2 ), 0, 0 ) );
   } else {
    AuthoringTool.getInstance().showErrorDialog( "Unable to create gui for variable: " + variable, null );
    }
   }

  this.add( javax.swing.Box.createVerticalStrut( 8 ), new java.awt.GridBagConstraints( 0, count++, 1, 1, 1.0, 0.0, java.awt.GridBagConstraints.WEST, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 2, 2, 2, 2 ), 0, 0 ) );

  for (java.util.Iterator iter = propertiesToWatch.iterator(); iter.hasNext(); ) {
   edu.cmu.cs.stage3.alice.core.Property property = (edu.cmu.cs.stage3.alice.core.Property)iter.next();
   edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory factory = new edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory( property );
   javax.swing.JComponent gui = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getPropertyGUI( property, true, false, factory );
   if (gui != null) {
    this.add( gui, new java.awt.GridBagConstraints( 0, count++, 1, 1, 1.0, 0.0, java.awt.GridBagConstraints.WEST, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 2, 2, 2, 2 ), 0, 0 ) );
   } else {
    AuthoringTool.getInstance().showErrorDialog( "Unable to create gui for property: " + property, null );
    }
   }

  java.awt.Component glue = javax.swing.Box.createGlue();
  this.add( glue, new java.awt.GridBagConstraints( 0, count++, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.WEST, java.awt.GridBagConstraints.BOTH, new java.awt.Insets( 2, 2, 2, 2 ), 0, 0 ) );

  revalidate();
  repaint();
  }

 }
